package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.audi.R;
import com.audi.activity.MoveDeviceInfoActivity;
import com.audi.activity.SelectDevicesActivity;
import com.audi.dto.DeviceItem;

public class SelectDevicesAdapter extends BaseAdapter {
	private Activity mActivity;
	private ArrayList<DeviceItem> mMoveDeviceCardList;
	// private CheckBox mCheckSelected;
	private ArrayList<DeviceItem> mListSelectDevice = new ArrayList<DeviceItem>();

	public SelectDevicesAdapter(Activity activity,
			ArrayList<DeviceItem> mMoveDeviceCardList) {
		mActivity = activity;
		this.mMoveDeviceCardList = mMoveDeviceCardList;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mMoveDeviceCardList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mMoveDeviceCardList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView,
			ViewGroup viewgroup) {
		// TODO Auto-generated method stub
		DeviceItem item = mMoveDeviceCardList.get(position);
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.row_item_select_devices, null);
			holder.imageDevice = (ImageView) convertView
					.findViewById(R.id.img_select_devices);
			holder.nameDevice = (TextView) convertView
					.findViewById(R.id.name_select_devices);
			holder.nameRoom = (TextView) convertView
					.findViewById(R.id.name_room_select_devices);
			holder.check = (CheckBox) convertView
					.findViewById(R.id.chk_select_devices);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// set data for List
		
		if (TextUtils.isEmpty(item.getDeviceIcon().trim())) {
			holder.imageDevice.setImageResource(R.drawable.icon_device_no_icon);
		} else {
			BitmapDrawable bitmapDrawable = new BitmapDrawable(
					mActivity.getResources(), item.getDeviceIcon());
			holder.imageDevice.setImageDrawable(bitmapDrawable);
		}

		holder.nameDevice.setText((CharSequence) item.getDeviceName());
		holder.nameRoom.setText((CharSequence) item.getDevicePlace());

		holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@SuppressWarnings("static-access")
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean checked) {
				if (checked) {
					mListSelectDevice.add(mMoveDeviceCardList.get(position));
					mMoveDeviceCardList.get(position).setIscheck(true);
					((SelectDevicesActivity)mActivity).checklistDevice(mMoveDeviceCardList.get(position));
					Log.d("Check=======","ADD_>>>>>"+ mMoveDeviceCardList.get(position));
				} else {
					mListSelectDevice.remove(mMoveDeviceCardList.get(position));
					mMoveDeviceCardList.get(position).setIscheck(false);
					((SelectDevicesActivity)mActivity).checklistDevice(mMoveDeviceCardList.get(position));
					Log.d("Check=======","REMOVE_>>>>>"+ mMoveDeviceCardList.get(position));
					
				}

			}
		});
		if (item.getIsCheck()) {
			holder.check.setChecked(true);
		} else {
			holder.check.setChecked(false);
		}

		return convertView;
	}

	public int getCheckDevice() {
		int result = 0;
		for (int i = 0; i < mMoveDeviceCardList.size(); i++) {
			if (mMoveDeviceCardList.get(i).getIsCheck()) {
				result++;
			}
		}
		return result;
	}

	public ArrayList<DeviceItem> getListDevice() {
		return mListSelectDevice;
	}

	static class ViewHolder {
		ImageView imageDevice;
		TextView nameDevice;
		TextView nameRoom;
		CheckBox check;
	}
}
